// pages/search/search.js

const app = getApp()
const api = require('../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        siteUrl: app.globalData.siteUrl,
        // 搜素
        value: '',
        // 列表数据
        list1: [],
        list2: [],
        list1Id: 1,
        // 切换置顶
        tabsList: [
            {
                title: '产品/服务'
            },
            {
                title: '企业'
            },
            {
                title: '会员'
            }
        ],
        tabsCurrent: -1,
        stickyScroll: false,
        // 分类弹窗
        typePopList: [
            
        ],
        typePopShow: false,
        // 历史搜索
        hisSearchList: [
            
        ],
        hisSearchCurrent: -1,
        // 热门推荐
        hotRecommendList: [
            
        ],
        hotRecommendCurrent: -1,
        enterprise: [],
        page:1,
        page1:1,
        page2:1,
        is_last1:false,
        is_last2:false,
        is_last3:false,
        page_num:10,
        address:[]
    },
    onLoad(options) {
        if(options.str){
          this.setData({
            value: options.str
          })
        }
       
        this.setData({
          tabsCurrent: options.tabsCurrent
        })
    },
    onShow(){
      let that = this
      this.setData({
        page:1,
        page1:1,
        page2:1,
        is_last1:false,
        is_last2:false,
        is_last3:false
      })
      this.product(that, 1);
      this.get_enterprise(that, 1);
      this.get_address(that, 1)
    },

    onSearch(){
      let that = this
      this.setData({
        page:1,
        page1:1,
        page2:1,
        is_last1:false,
        is_last2:false,
        is_last3:false
      })
      this.product(that, 1);
      this.get_enterprise(that, 1);
      this.get_address(that, 1)
    },
    onChange(e){
      let that = this
      this.setData({
        value: e.detail
      })
      if(e.detail == ''){
        this.setData({
          page:1,
          page1:1,
          page2:1,
          is_last1:false,
          is_last2:false,
          is_last3:false
        })
        this.product(that, 1);
        this.get_enterprise(that, 1);
        this.get_address(that, 1)
      }
    },
    // 刷新新瀑布流
    update(isFlag) {
      // 重置瀑布流组件
      if(isFlag){
        // this.selectComponent('.waterfall').reset()
      }
    
      let list1 = this.getMockData()
      this.setData({ list1 },()=> {
        console.log(this.data.list1)
        wx.hideLoading();
        wx.stopPullDownRefresh()
      })
      //this.selectComponent('#waterfall').reset()
     
  },

    // 获取瀑布流数据
    getMockData() {
      let { list1Id } = this.data
      let list1 = []
      for (let i = 0; i < this.data.list2.length; i++) {
          list1.push({
              list1Id,
              imgUrl: app.globalData.siteUrl + this.data.list2[i].url,
              title: this.data.list2[i].title+'',
              price: this.data.list2[i].price+'',
              companyLogoUrl: app.globalData.siteUrl + this.data.list2[i].companyLogoUrl,
              companyTitle: this.data.list2[i].companyTitle+'',
              tage: this.data.list2[i].tag+'',
              is_discuss: this.data.list2[i].is_discuss+'',
              id: this.data.list2[i].id,
              sub_title:this.data.list2[i].sub_title,
          })

          this.data.list1Id = ++list1Id
      }
      return list1
  },
    // 切换置顶
    onStickyScroll(e) {
        let stickyScroll = false
        if(e.detail.isFixed) {
            stickyScroll = !stickyScroll
        }
        this.setData({stickyScroll})
    },
    // 切换
    onTabsClick(e) {
        let tabsCurrent = e.target.dataset.index
        this.setData({tabsCurrent})
    },
   
    //获取产品
  product(that, page){
    wx.showLoading()
    let param = {
      str: that.data.value,
      category: ''
    }
    api.product_listp(param, page).then(function(res){
      
      if (res.data.length>0){
        let list2 = []
        for(let i = 0; i< res.data.length; i++){
          list2.push({
            url: res.data[i].url,
            title: res.data[i].title+'',
            price: res.data[i].price+'',
            companyLogoUrl: res.data[i].logo,
            companyTitle: res.data[i].companyName+'',
            tag: res.data[i].category+'',
            is_discuss: res.data[i].is_discuss+'',
            id: res.data[i].id,
            sub_title: res.data[i].sub_title
          })
        }
        that.setData({
          list2: page == 1 ? list2 : that.data.list2.concat(list2),//合并数据
          page: page + 1
       },()=>{
         if(page > 1 ){
           that.update(false)
         }else{
           that.update(true)
         }
        
       })
        
      }else{
        that.setData({
          is_last1: true
        })
        wx.hideLoading();
          wx.showToast({
            title: '没有更多数据了',
            ico: 'error'
          })
      }
    
    })
  },

  get_enterprise(that, page){
    wx.showLoading();
    let param = {
      str: that.data.value,
      page_num: this.data.page_num,
      category: ''
    }
    api.enterprise_list_page(param, page).then(function(res){
      if (res.data.length>0){
        that.setData({
          enterprise: page == 1 ? res.data : that.data.enterprise.concat(res.data),//合并数据
          page1: page + 1
        })
      }else{
        that.setData({
          is_last2: true
        })
          wx.showToast({
            title: '没有更多数据了',
            ico: 'error'
          })
      }
      wx.hideLoading();
    })
  },

  get_address(that, page){
    wx.showLoading();
    let param = {
      str: that.data.value,
    }
    api.address_list_page(param, page).then(function(res){
      console.log(res.data)
      if (res.data.length>0){
        that.setData({
          address: page == 1 ? res.data : that.data.address.concat(res.data),//合并数据
          page2: page + 1
        })
      }else{
        that.setData({
          is_last3: true
        })
          wx.showToast({
            title: '没有更多数据了',
            ico: 'error'
          })
      }
      wx.hideLoading();

    })
  },


  onPullDownRefresh:function(){
    this.onRefresh();
  
  },
  onRefresh:function(){
      let that = this
      //导航条加载动画
      //wx.showNavigationBarLoading();
      setTimeout(function () {
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        that.setData({
          page: 1,
          page1: 1,
          page2:1,
          list1Id: 1,
          is_last1:false,
          is_last2:false,
          is_last3:false
        })
        that.product(that, 1);
        that.get_enterprise(that, 1);
        that.get_address(that, 1)
      }, 2000);
    },

    onReachBottom:function(){
      // 加载更多，滑动到底部执行此方法
      if(!this.data.is_last1 && this.data.tabsCurrent == 0){
        let page = this.data.page ;
        console.log(page)
        let that = this
        this.product(that, page)
      }
      if(!this.data.is_last2 && this.data.tabsCurrent == 1){
        let page1 = this.data.page1 ;
        let that = this
        this.get_enterprise(that, page1)
      }
      if(!this.data.is_last3 && this.data.tabsCurrent == 2){
        let page2 = this.data.page2 ;
        let that = this
        this.get_address(that, page2)
      }
  }
})